package hot100;

public class Lc104 {
    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
/* 方法1，求每一个节点所在的深度，然后求最大值
    private int ret = 0;

    public int maxDepth(TreeNode root) {
        dfs(root, 0);
        return this.ret;
    }

    void dfs(TreeNode root, int cur_dep) {
        if (root == null) {
            return;
        }

        cur_dep += 1;
        this.ret = Math.max(this.ret, cur_dep);
        dfs(root.left, cur_dep);
        dfs(root.right, cur_dep);
    }
 */

    // 方法2，子任务递归
    public int maxDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        if (root.left == null && root.right == null) {
            return 1;
        }
        return Math.max(this.maxDepth(root.left), this.maxDepth((root).right)) + 1;
    }
}
